<template>
  <div>
    <common-head title="商品列表" v-if="isShow" class="co-head" />
    <!-- 销量价格排序 -->
    <div class="order-wrap " :style="{ top: top }">
      <div
        :class="[
          'sale',
          'filter',
          {
            active: order.by === 'sale',
          },
        ]"
        @click="bySale"
      >
        <span>销量</span>
        <div
          :class="[
            'top',
            {
              active: order.by === 'sale' && order.updown == 1,
            },
          ]"
        ></div>
        <div
          :class="[
            'bottom',
            {
              active: order.by === 'sale' && order.updown == 0,
            },
          ]"
        ></div>
      </div>
      <div
        :class="[
          'price',
          'filter',
          {
            active: order.by === 'price',
          },
        ]"
        @click="byPrice"
      >
        <span>价格</span>
        <div
          :class="[
            'top',
            {
              active: order.by === 'price' && order.updown == 1,
            },
          ]"
        ></div>
        <div
          :class="[
            'bottom',
            {
              active: order.by === 'price' && order.updown == 0,
            },
          ]"
        ></div>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="item-wrap">
      <div class="home-recommend" v-for="item in items" :key="item.id">
        <div class="reco-left">
          <img :src="item.pic" alt="" width="200px" />
        </div>
        <div class="reco-right">
          <h3 clsss="title">{{ item.name }}</h3>
          <p class="desc">{{ item.tags ? item.tags : "暂无描述" }}</p>
          <div class="buttom">
            <span class="price"> ￥{{ item.minPrice }} </span>
            <van-icon
              name="gouwuche1"
              class-prefix="icon"
              class="xianhua"
            ></van-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
// 将请求接口引入
import { fetchItems } from '@api'
export default {
  data () {
    return {
      order: {
        by: 'sale', // 默认是按照销量排序，并且是降序
        updown: 0 // 0表示降序，1表示升序
      },
      orderBy: 'ordersDown', // 默认是销量降序
      top: '47px', // 销量价格排序的的盒子距离上方定位的距离
      isShow: true, // 用来表示在滚动的时候，上方的通栏是显示还是消失
      items: []// 用来存放下方的列表请求过来的数据
    }
  },
  mounted () {
    this.onScoll()
  },
  beforeDestroy () {
    window.onscroll = null
  },
  created () {
    this.fetchItems()
  },
  methods: {
    bySale () {
      if (this.order.by === 'price') {
        this.order.by = 'sale'
        this.order.updown = 0
        this.orderBy = 'ordersDown'
      } else {
        if (this.order.updown === 0) {
          this.order.updown = 1
          this.orderBy = 'ordersUp'
        } else {
          this.order.updown = 0
          this.orderBy = 'ordersDown'
        }
      }
      this.fetchItems()
    },
    byPrice () {
      if (this.order.by === 'sale') {
        this.order.by = 'price'
        this.order.updown = 1
        this.orderBy = 'priceUp'
      } else {
        if (this.order.updown === 0) {
          this.order.updown = 1
          this.orderBy = 'priceUp'
        } else {
          this.order.updown = 0
          this.orderBy = 'priceDown'
        }
      }
      this.fetchItems()
    },
    // 添加滚动事件
    onScoll () {
      window.onscroll = () => {
        const sTop =
          document.documentElement.scrollTop || document.body.scrollTop
        if (sTop > 47) {
          this.isShow = false
          this.top = 0
        } else {
          this.isShow = true
          this.top = '47px'
        }
      }
    },
    // 请求商品列表数据
    fetchItems () {
      fetchItems({
        categoryId: this.$route.params.cateId,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.item-wrap {
  padding-top: 106px;
}
.order-wrap {
  background: #fff;
  border-top: 1px solid #ccc;
  // top: 46px;
  height: 50px;
  line-height: 50px;
  position: fixed;
  display: flex;
  width: 100%;
  justify-content: space-around;
  .filter {
    position: relative;
    &.active {
      color: #fb9998;
    }
    .top {
      position: absolute;
      width: 0;
      height: 0;
      border: 5px solid transparent;
      border-bottom-color: #777;
      top: 12px;
      right: -15px;
      &.active {
        border-bottom-color: #fb9998;
      }
    }
    .bottom {
      position: absolute;
      width: 0;
      height: 0;
      border: 5px solid transparent;
      border-top-color: #777;
      top: 26px;
      right: -15px;
      &.active {
        border-top-color: #fb9998;
      }
    }
  }
}

.home-recommend {
  display: flex;
  margin-bottom: 10px;
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  .reco-right {
    flex: 1;
    padding: 15px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .buttom {
      display: flex;
      .price {
        font-weight: bold;
        flex: 1;
      }
      .xianhua {
        background: #fb9998;
        padding: 3px;
        color: #fff;
        border-radius: 50%;
      }
    }
  }
}
</style>
